//
//  IWFriendshipCell.m
//  ItcastWeibo
//
//  Created by mj on 14-1-15.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

#import "IWFriendshipCell.h"
#import "AyIconView.h"
#import "AyUser.h"
#import "AyStatus.h"
#import "UIImageExtend.h"
#import "AyDefine.h"


@interface IWFriendshipCell()
{
    UIView *_divider;
    /** 背景view */
    UIImageView *_bgView;
    /** 选中的背景view */
    UIImageView *_selectedBgView;
}
@property (weak, nonatomic) IBOutlet AyIconView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *statusLabel;
@property (weak, nonatomic) IBOutlet UIButton *followBtn;
@end

@implementation IWFriendshipCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"friendship";
    IWFriendshipCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"IWFriendshipCell" owner:nil options:nil][0];
    }
    return cell;
}

- (void)awakeFromNib
{
    // 1.按钮背景
    [_followBtn setBackgroundImage:[UIImage resizedImageWithName:@"userinfo_relationship_button_background"] forState:UIControlStateNormal];
    [_followBtn setBackgroundImage:[UIImage resizedImageWithName:@"userinfo_relationship_button_highlighted"] forState:UIControlStateHighlighted];

    
    UIImageView *bg = [[UIImageView alloc] init];
    bg.image=[UIImage resizedImageWithName:@"common_card_background_highlighted"];
    self.backgroundView = bg;

    UIImageView *selectedBg = [[UIImageView alloc] init];
    bg.image=[UIImage resizedImageWithName:@"common_card_background"];
    self.selectedBackgroundView = selectedBg;
    
}

- (void)setFrame:(CGRect)frame
{
    frame.origin.y += AyStatusTableBorder*0.8;
    frame.origin.x = AyStatusTableBorder;
    frame.size.width -= 2 * AyStatusTableBorder;
    frame.size.height -= AyStatusTableBorder;
    [super setFrame:frame];
}


- (void)setUser:(AyUser *)user
{
    _user = user;
    
    // 0.头像
    [_iconView setUser:user iconType:IWIconTypeDefault];
    
    // 1.昵称
    _nameLabel.text = user.name;
    
    // 2.最近的微博
    _statusLabel.text = user.status.text;
    
    // 3.按钮
    if (user.following) { // 已经关注
        [_followBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [_followBtn setTitle:@"已关注" forState:UIControlStateNormal];
        [_followBtn setImage:[UIImage imageWithName:user.follow_me?@"userinfo_relationship_indicator_arrow":@"userinfo_relationship_indicator_tick_unfollow"] forState:UIControlStateNormal];
    } else { // 未关注
        [_followBtn setTitleColor:AyColor(42, 135, 46) forState:UIControlStateNormal];
        [_followBtn setTitle:@"关注" forState:UIControlStateNormal];
        [_followBtn setImage:[UIImage imageWithName:@"userinfo_relationship_indicator_plus"] forState:UIControlStateNormal];
    }
}
+ (CGFloat)cellHeight
{
    return 75;
}

- (void)setIndexPath:(NSIndexPath *)indexPath
{
    _indexPath = indexPath;
    
    _divider.hidden = indexPath.row == 0;
}

@end
